package com.gewu.pm.ui.fragment.approval;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.bean.ApprovalApplicationBean;
import com.gewu.pm.ui.activity.approval.ApprovalDetailActivity;
import com.gewu.pm.ui.adapter.approval.ApprovalApplicationAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的审批 Fragment
 */
public final class MyApprovalFragment extends AppFragment<AppActivity>
        implements BaseAdapter.OnItemClickListener {

    private RecyclerView mRecyclerView;
    private ApprovalApplicationAdapter mAdapter;
    private List<ApprovalApplicationBean> mDataList = new ArrayList<>();

    public static MyApprovalFragment newInstance() {
        return new MyApprovalFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_application;
    }

    @Override
    protected void initView() {
        mRecyclerView = findViewById(R.id.rv_list);
        
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new ApprovalApplicationAdapter(getContext());
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        loadData();
    }

    /**
     * 加载数据（创建测试数据）
     */
    private void loadData() {
        mDataList.clear();
        
        // 创建测试数据：待审批
        ApprovalApplicationBean bean1 = new ApprovalApplicationBean(
                "1",
                "买赠活动",
                "购买祖名老豆腐4000g3盒，赠送祖名浓浆豆腐400g1盒",
                1  // 待审批
        );
        
        // 已通过
        ApprovalApplicationBean bean2 = new ApprovalApplicationBean(
                "2",
                "买赠活动",
                "购买祖名老豆腐4000g3盒，赠送祖名浓浆豆腐400g1盒",
                2  // 已通过
        );
        
        // 已驳回
        ApprovalApplicationBean bean3 = new ApprovalApplicationBean(
                "3",
                "买赠活动",
                "购买祖名老豆腐4000g3盒，赠送祖名浓浆豆腐400g1盒",
                3  // 已驳回
        );
        
        mDataList.add(bean1);
        mDataList.add(bean2);
        mDataList.add(bean3);
        
        mAdapter.setData(mDataList);
    }

    /**
     * 更新筛选数据
     */
    public void updateFilter(int type) {
        // 根据类型筛选数据
        if (type == 0) {
            // 全部
            mAdapter.setData(mDataList);
        } else {
            // 按状态筛选
            List<ApprovalApplicationBean> filterList = new ArrayList<>();
            for (ApprovalApplicationBean bean : mDataList) {
                if (bean.getType() == type) {
                    filterList.add(bean);
                }
            }
            mAdapter.setData(filterList);
        }
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        ApprovalApplicationBean bean = mAdapter.getItem(position);
        // 跳转到详情页，传递状态类型
        ApprovalDetailActivity.start(getContext(), bean.getType());
    }
}

